package com.zhanghaijun.createCode.ad.service;


import com.zhanghaijun.createCode.ad.bean.AdSynchronousBean;
import com.zhanghaijun.createCode.ad.bean.AdUser;
import com.zhanghaijun.createCode.ad.bean.OuBean;
import com.zhanghaijun.createCode.ad.bean.SearchOuUserResultBean;
import com.zhanghaijun.createCode.ad.util.AdDoActionUtil;
import com.zhanghaijun.createCode.util.JsonResultDto;
import org.jsoup.Jsoup;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhanghaijun on 15/6/10.
 */
@Service
public class AdApiService {

    private static final String SEND_MAIL_PATH="http://192.168.59.104:2005/monitor/tool/mail.do";

    /**
     * 添加用户
     * 初始化用户期间调用
     */
    public JsonResultDto addAdUser(AdSynchronousBean adSynchronousBean) {
        try{
            String loginName = adSynchronousBean.getAdUser().getLoginName();
            AdUser adUser = adSynchronousBean.getAdUser();
            SearchOuUserResultBean searchOuUserResultBean = AdDoActionUtil.searchOuUserByUserPrincipalName(loginName);
            if (searchOuUserResultBean != null) {
                return new JsonResultDto(true, 101, "aduser have already exist");
            }
            AdDoActionUtil.createRootOu();
            String company = adSynchronousBean.getCompany();//公司
            String bussinessDept = adSynchronousBean.getBussinessDept();//事业部
            String orgStr = adSynchronousBean.getOrg();//部门

            String[] ouArray = new String[]{company, bussinessDept, orgStr};

            String[] ouFinalArray = addOrgOUAndReturnOuDistinguishedName(ouArray);//得到的是过滤之后的ou array
            String cnDistinguishedName = AdDoActionUtil.createCnDistinguishedName(ouFinalArray, adUser.getCn());
            boolean cnRes2 = AdDoActionUtil.searchByDistinguishedName(cnDistinguishedName);
            if (!cnRes2) {
                adUser.setSubContext(cnDistinguishedName);
                JsonResultDto res = AdDoActionUtil.add(adUser);
                if(res.isSuccess()){
                    return new JsonResultDto(true);
                }
            }else{
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("mail", adUser.getMail());
                updateUserProperty(loginName, params);
            }
            return new JsonResultDto(false,101,"add false");
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResultDto(false);
        }
    }


    /**
     * 禁用或者开启AD用户
     * 1禁用 0 启用
     */
    public JsonResultDto disabledOrOpenAdUser(String loginName,int isStop) {
        Map<String, Object> params = new HashMap<String, Object>();
        if(isStop==1){
            params.put("userAccountControl", "514");
        }else{
            params.put("userAccountControl", "544");
        }

        return updateUserProperty(loginName, params);
    }

    /**
     * 修改AD用户属性
     * @param loginName
     * @param params
     * @return
     * 主要属性有
     * displayName
     * distinguishedName
     * userAccountControl 514 禁用 544 启用
     * mail 邮箱
     * streetAddress等
     * 用法详见@disabledAdUser()方法
     */
    public JsonResultDto updateUserProperty(String loginName, Map<String, Object> params) {
        return AdDoActionUtil.update(loginName, params);
    }

    /**
     * 换岗位
     * @param adSynchronousBean
     * @return
     */
    public JsonResultDto exchangePost(AdSynchronousBean adSynchronousBean) {
        String loginName = adSynchronousBean.getAdUser().getLoginName();
        AdUser adUser = adSynchronousBean.getAdUser();
        SearchOuUserResultBean searchOuUserResultBean = AdDoActionUtil.searchOuUserByUserPrincipalName(loginName);
        if (searchOuUserResultBean == null) {
            return new JsonResultDto(false, 101, "aduser is not in AD please import to ad");
        }

        AdDoActionUtil.createRootOu();
        String company = adSynchronousBean.getCompany();//公司
        String bussinessDept = adSynchronousBean.getBussinessDept();//事业部
        String orgStr = adSynchronousBean.getOrg();//部门
        String[] ouArray = new String[]{company, bussinessDept, orgStr};
        String[] ouFinalArray = addOrgOUAndReturnOuDistinguishedName(ouArray);//得到的是过滤之后的ou array

        String oldData = searchOuUserResultBean.getDistinguishedName();
        String newData = AdDoActionUtil.createCnDistinguishedName(ouFinalArray, adUser.getCn());
        JsonResultDto result =  AdDoActionUtil.reNameDistinguishedName(oldData, newData);
        return result;
    }

    /**
     * 登陆
     * @param loginName
     * @param pwd
     * @return
     */
    public boolean loginByAd(String loginName,String pwd){
        boolean loginRes = AdDoActionUtil.authenticateUserNameAndPwd(loginName,pwd);
        return  loginRes;
    }


    /**
     * 修改密码
     * @return
     */
    public JsonResultDto changePassword(String loginName,String oldPassword,String newPassword){
        return AdDoActionUtil.changePassword(loginName,oldPassword, newPassword);
    }

    /**
     * 修改邮件
     * @return
     */
    public JsonResultDto changeMail(String loginName,String mail){
        Map<String, Object> params = new HashMap<String,Object>();
        params.put("mail",mail);
        return AdDoActionUtil.update(loginName, params);
    }


    /**
     * 遍历公司 事业部 部门
     * 存在跳过 不存在 保存
     * 遇到null 直接跳出循环
     * 返回过滤后的ou array
     *
     * @return
     */
    private String[] addOrgOUAndReturnOuDistinguishedName(String[] ouArray) {

        int arrSize =0;
        for (String key : ouArray) {
            if (key == null) {
                break;
            }
            arrSize++;
        }
        String[] notNullOuArray = new String[arrSize];
        int index = 0;
        for (String key : ouArray) {
            if (key == null) {
                break;
            }
            notNullOuArray[index] = key;
            String ouDistinguishedName = AdDoActionUtil.createOUDistinguishedName(notNullOuArray);
            boolean res = AdDoActionUtil.searchByDistinguishedName(ouDistinguishedName);
            if (!res) {
                OuBean ouBean = new OuBean();
                ouBean.setDescription(key);
                ouBean.setDisplayName(key);
                ouBean.setOu(key);
                ouBean.setSubContext(ouDistinguishedName);
                AdDoActionUtil.addOu(ouBean);
            }
            index++;
        }
        return notNullOuArray;
    }


    public static void main(String[] args){
        AdApiService adApiService = new AdApiService();
        AdSynchronousBean adSynchronousBean = new AdSynchronousBean();
        adSynchronousBean.setCompany("恩牛网络");
        adSynchronousBean.setBussinessDept("产品中心");
        adSynchronousBean.setOrg("人品事业部");

        AdUser adUser = new AdUser();
        adUser.setLoginName("liuchunchuang");
        adUser.setCn("刘春闯");
        adSynchronousBean.setAdUser(adUser);
        adApiService.exchangePost(adSynchronousBean);

    }




}
